package ecoiot.com.testproject.listview;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.yydcdut.sdlv.Menu;
import com.yydcdut.sdlv.MenuItem;
import com.yydcdut.sdlv.SlideAndDragListView;

import java.util.ArrayList;
import java.util.List;

import ecoiot.com.testproject.R;

/**
 * Created by developer on 17-8-24.
 */

public class TestListViewActivity extends AppCompatActivity {


    private SwipeMenuListView smlv;
    private SlideAndDragListView sadlv;
    private ArrayList<String> list;
    private MyAdapter adapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_listview);

        smlv = (SwipeMenuListView) findViewById(R.id.activity_test_listview_lv1);
        sadlv = (SlideAndDragListView) findViewById(R.id.activity_test_listview_lv2);
        list = new ArrayList<>();
        for (int i = 0; i < 10; i ++) {
            String str = "This is the" + i + " item";
            list.add(str);
        }

        adapter = new MyAdapter();

        initSwipeMenuListView();

        initSlideAndDragListView();


    }

    private void initSlideAndDragListView() {
        Menu menu = new Menu(false, 0);//第1个参数表示滑动 item 是否能滑的过头，像弹簧那样( true 表示过头，就像 Gif 中显示的那样；false 表示不过头，就像 Android QQ 中的那样)
        menu.addItem(new MenuItem.Builder().setWidth(dp2px(90))//单个菜单 button 的宽度
                .setBackground(new ColorDrawable(Color.RED))//设置菜单的背景
                .setText("Delete")//set text string
                .setTextColor(Color.GRAY)//set text color
                .setTextSize(20)//set text size
                .setDirection(MenuItem.DIRECTION_RIGHT)
//                .setIcon(getResources().getDrawable(R.drawable.ic_launcher))// set icon
                .build());

        sadlv.setMenu(menu);
        sadlv.setOnMenuItemClickListener(new SlideAndDragListView.OnMenuItemClickListener() {
            @Override
            public int onMenuItemClick(View v, final int itemPosition, int buttonPosition, int direction) {
                switch (direction) {
                    case MenuItem.DIRECTION_LEFT:
                        switch (buttonPosition) {
                            case 0://One
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(TestListViewActivity.this, "Delete", Toast.LENGTH_SHORT).show();
                                        list.remove(itemPosition);
                                        adapter.notifyDataSetChanged();

                                    }
                                });
                                return Menu.ITEM_SCROLL_BACK;
                        }
                        break;
                    default :
                        return Menu.ITEM_SCROLL_BACK;
                }
                return Menu.ITEM_NOTHING;
            }
        });

        sadlv.setOnDragDropListener(new SlideAndDragListView.OnDragDropListener() {
            @Override
            public void onDragViewStart(int beginPosition) {
                mTempString = list.get(beginPosition);
            }

            @Override
            public void onDragDropViewMoved(int fromPosition, int toPosition) {
                String str = list.remove(fromPosition);
                list.add(toPosition, str);
            }

            @Override
            public void onDragViewDown(int finalPosition) {
                list.set(finalPosition, mTempString);
            }
        });

        sadlv.setOnItemDeleteListener(new SlideAndDragListView.OnItemDeleteListener() {
            @Override
            public void onItemDeleteAnimationFinished(View view, int position) {
                list.remove(position - sadlv.getHeaderViewsCount());
                adapter.notifyDataSetChanged();
            }
        });

        sadlv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                return true;
            }
        });

        sadlv.setAdapter(adapter);

    }


   /* private View.OnTouchListener mOnTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Object o = v.getTag();
            if (o != null && o instanceof Integer) {
                sadlv.startDrag(((Integer) o).intValue());
            }
            return false;
        }
    };*/

    String mTempString;


    private void initSwipeMenuListView() {
        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {
                // create "open" item
               /* SwipeMenuItem openItem = new SwipeMenuItem(
                        getApplicationContext());
                // set item background
                openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
                        0xCE)));
                // set item width
                openItem.setWidth(dp2px(90));
                // set item title
                openItem.setTitle("Open");
                // set item title fontsize
                openItem.setTitleSize(18);
                // set item title font color
                openItem.setTitleColor(Color.WHITE);
                // add to menu
                menu.addMenuItem(openItem);*/

                // create "delete" item
                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        getApplicationContext());
                // set item background
//                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
//                        0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(dp2px(90));
                // set a icon
                deleteItem.setIcon(R.drawable.ic_launcher);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };

        smlv.setMenuCreator(creator);
        smlv.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(final int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(TestListViewActivity.this, "Delete", Toast.LENGTH_SHORT).show();
                                list.remove(position);
                                adapter.notifyDataSetChanged();
                            }
                        });
                        break;
                }
                // false : close the menu; true : not close the menu
                return false;
            }
        });


        smlv.setAdapter(adapter);
        // Right
        smlv.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);
    }

    public int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }


    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view;
            ViewHolder holder;
            if (convertView == null) {
                view = View.inflate(TestListViewActivity.this,R.layout.item_drag_list,null);
                holder = new ViewHolder();
                holder.textView = (TextView) view.findViewById(R.id.item_drag_tv);
                holder.btn = (Button) view.findViewById(R.id.item_drag_btn);
                view.setTag(holder);
            }else {
                view = convertView;
                holder = (ViewHolder) view.getTag();
            }

            holder.textView.setText(list.get(position));
            holder.btn.setText(position + "");
            holder.btn.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            sadlv.startDrag(position);
                        }
                    });
                    return true;
                }
            });


            return view;
        }

        private class ViewHolder {
            TextView textView;
            Button btn;
        }


    }



}
